/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.simulation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public class FutureQueue {
	private final SortedSet<SimEvent> futureEvents = new TreeSet<SimEvent>();
	private long serial = 0;
	
	public List<SimEvent> get(int id, int tag){
		List<SimEvent> searchedEvents = new ArrayList<SimEvent>();
		Iterator<SimEvent> futureEventItr = futureEvents.iterator();
		while(futureEventItr.hasNext()){
			SimEvent event = futureEventItr.next();
			if(event.getDst()==id && event.getTag()==tag){
				searchedEvents.add(event);
			}
		}
		return searchedEvents;
	}
	
	public void addEvent(SimEvent event){
		event.setSerial(serial++);
		futureEvents.add(event);
	}
	
	public boolean remove(SimEvent event){
		return futureEvents.remove(event);
	}
	
	public boolean removeAll(Collection<SimEvent> events){
		return futureEvents.removeAll(events);
	}
	
	public int size(){
		return futureEvents.size();
	}
	
	public Iterator<SimEvent> iterator(){
		return futureEvents.iterator();
	}
	
	public void clear(){
		futureEvents.clear();
	}

	/**
	 * @return the sortedFutureQueue
	 */
	public SortedSet<SimEvent> getSortedFutureQueue() {
		return futureEvents;
	}

	/**
	 * @return the serial
	 */
	public long getSerial() {
		return serial;
	}

	/**
	 * @param serial the serial to set
	 */
	public void setSerial(long serial) {
		this.serial = serial;
	}

}
